<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :props=" {
            children: 'children',
            label: 'label'
          }"
          default-expand-all
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>

                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>

                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog :title="isEdit?'编辑':'增加'" :visible.sync="showDialog" @close="hClose">
      <AddorEdit v-if="showDialog" :id="curId" ref="form" :origin-list="originList" :is-edit="isEdit" @hSuccess="hSuccess" />
    </el-dialog>
  </div>
</template>
<script>
import AddorEdit from './deptDialog.vue'
import { getDepartments, delDepartmentById } from '@/api/department'
import { transDataToTreeData } from '@/utils/index'
export default {
  components: { AddorEdit },
  data() {
    return {
      list: [], // 用来获取企业的部门列表.树形图
      showDialog: false, // 用来控制弹框的显示与隐藏
      curId: '', // 传递给子组件的ID
      isEdit: false, // 用来控制是编辑状态还是增加状态
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 重置表单
    // 点击子组件取消按钮 ,关闭弹窗
    hClose() {
      this.showDialog = false
    },
    // 发送请求成功之后的1操作
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    // 新增部门
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 修改编辑部门
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
    },
    // 删除指定部门
    async hDel(id) {
      try {
        const res = await delDepartmentById(id)
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadDepartments()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取企业的部门列表
    async loadDepartments() {
      const res = await getDepartments()
      console.log(res)

      res.data.depts.shift()
      this.originList = res.data.depts.map(item => { return { code: item.code, id: item.id, name: item.name, pid: item.pid } })
      console.log('originList', this.originList)
      this.list = transDataToTreeData(res.data.depts)
    }
  }
}
</script>
